/*
 * Copyright 2019 http://www.whcrow.com All rights reserved.
 */

package com.whcrow.example.mybatisplus.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * MyMetaObjectHandler
 *
 * @author whcrow
 * @version 1.0.0
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    private static final String FIELD_NAME_CREATE_DATE = "createDate";
    private static final String FIELD_NAME_UPDATE_DATE = "updateDate";

    @Override
    public void insertFill(MetaObject metaObject) {
        LocalDateTime now = LocalDateTime.now();
//        List<StrictFill> insertFillList = new ArrayList<>(2);
//        insertFillList.add(StrictFill.of(FIELD_NAME_CREATE_DATE, LocalDateTime.class, now));
//        insertFillList.add(StrictFill.of(FIELD_NAME_UPDATE_DATE, LocalDateTime.class, now));
//        this.strictInsertFill(findTableInfo(metaObject), metaObject, insertFillList);
        this.setInsertFieldValByName(FIELD_NAME_CREATE_DATE, now, metaObject);
        this.setInsertFieldValByName(FIELD_NAME_UPDATE_DATE, now, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        LocalDateTime now = LocalDateTime.now();
//        List<StrictFill> updateFillList = new ArrayList<>(1);
//        updateFillList.add(StrictFill.of(FIELD_NAME_UPDATE_DATE, LocalDateTime.class, now));
//        this.strictUpdateFill(findTableInfo(metaObject), metaObject, updateFillList);
        this.setUpdateFieldValByName(FIELD_NAME_UPDATE_DATE, now, metaObject);
    }

}
